package icu.ac.kr.phonebook.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.util.Log;

public class DBProvider extends ContentProvider {

	static String TAG = "DBProvider";
	
	static final UriMatcher MATCHER;
	static final int TOTALSCHEDULE = 1; 
	static final int DAYMODE = 2;
	static final int MONTHMODE = 3;
	
	static{
		MATCHER = new UriMatcher(0);
		MATCHER.addURI(ConstantProvider.AUTHORITY, "schedule", TOTALSCHEDULE);
		MATCHER.addURI(ConstantProvider.AUTHORITY, "schedule/#", DAYMODE);
		MATCHER.addURI(ConstantProvider.AUTHORITY, "monthmode", MONTHMODE);
	}
	
	SQLiteDatabase sqlDB;
	
	@Override
	public int delete(Uri uri, String where, String[] arg2) {
		// TODO Auto-generated method stub
	
		Log.d(TAG, "deleted "+ uri.toString());
		
		switch(MATCHER.match(uri))
		{
		case TOTALSCHEDULE:
			Log.d(TAG, "deleted "+ where);
			sqlDB.delete("diary", where, null);
			break;
		
		}
		
		getContext().getContentResolver().notifyChange(uri, null);
		
		return 0;
	}

	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		
		Uri notiuri = null;
		long id = -1;
		switch(MATCHER.match(uri))
		{
		case TOTALSCHEDULE:
			
			id = sqlDB.insert("diary", null, values);
			notiuri = ContentUris.withAppendedId(uri, id);
			break;
		case MONTHMODE:
			id = sqlDB.insert("month_diary_security", null, values);
			notiuri = ContentUris.withAppendedId(uri, id);
			break;
			
		}
		
		getContext().getContentResolver().notifyChange(notiuri, null);
		
		Log.d(TAG, "INSERTING");
		
		return notiuri;
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		
		SQLOpenHelper sqlOpener = new SQLOpenHelper(getContext(), "diary.db", null, 1);
		sqlDB = sqlOpener.getWritableDatabase();
		
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
	
		Log.d(TAG, "QUERYING");
		
		Cursor cursor = null;
		switch(MATCHER.match(uri))
		{
		case TOTALSCHEDULE:
			cursor =sqlDB.query("diary", projection, selection, null, null, null, sortOrder);
			break;
		case MONTHMODE:
			cursor = sqlDB.query("month_diary_security", projection, selection, null, null, null, null);
			break;
			
		}

		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
	
		switch(MATCHER.match(uri))
		{
		case TOTALSCHEDULE:
			Log.d(TAG, "day mode updated");
			sqlDB.update("diary", values, selection, null);
			break;
		case DAYMODE:
			Log.d(TAG, "time mode updated");
			sqlDB.update("diary", values, selection, null);
			break;
		case MONTHMODE:
			Log.d(TAG, "date mode updated");
			sqlDB.update("month_diary_security", values, selection, null);
			break;
	
			
		}
		
		getContext().getContentResolver().notifyChange(uri, null);
		
		return 0;
	}

	
	private class SQLOpenHelper extends SQLiteOpenHelper{

		public SQLOpenHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			
			db.execSQL("create table diary(_id integer primary key, year integer, month integer, date integer, hour integer, type text, comment text, mode integer(1) default '0', unique('year','month','date','hour') );");
			db.execSQL("create table month_diary_security(_id integer primary key, year integer, month integer, date integer, mode integer(1) default '3',unique('year', 'month', 'date'));"); // 0:private, 1:public, 2:couple, 3:sharable
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			
			onCreate(db);
		}
		
	}
	
	
}
